<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="filter-container">
          <div class="demo-input-suffix acea-row">
            <span class="seachTiele">状态：</span>
            <el-select v-model="tableFrom.status" placeholder="请选择" class="filter-item selWidth mr20"
              @change="seachList" clearable>
              <el-option label="未开启" :value="0" />
              <el-option label="开启" :value="1" />
            </el-select>
            <span class="seachTiele">优惠券名称：</span>
            <el-input v-model="tableFrom.name" placeholder="请输入优惠券名称" class="selWidth" clearable>
              <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
            </el-input>
          </div>
        </div>
        <router-link :to="{ path: '/marketing/coupon/list/save' }">
          <el-button size="small" type="primary" v-hasPermi="['admin:coupon:save']">添加优惠劵</el-button>
        </router-link>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini"
        :header-cell-style="{ fontWeight: 'bold' }">
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column prop="name" label="名称" min-width="180" />
        <el-table-column label="类型" min-width="80">
          <template slot-scope="{ row }">
            <span>{{ row.useType | couponUserTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="money" label="面值" min-width="100" />
        <el-table-column prop="name" label="领取方式" min-width="100">
          <template slot-scope="{ row }">
            <span>{{ row.type | couponTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="260" label="领取日期">
          <template slot-scope="{ row }">
            <div v-if="row.receiveEndTime">
              {{ row.receiveStartTime }} - {{ row.receiveEndTime }}
            </div>
            <span v-else>不限时</span>
          </template>
        </el-table-column>
        <el-table-column min-width="260" label="使用时间">
          <template slot-scope="{ row }">
            <div v-if="row.day">
              {{ row.day }}天
            </div>
            <span v-else>
              {{ row.useStartTime }} - {{ row.useEndTime }}
            </span>
          </template>
        </el-table-column>
        <el-table-column min-width="100" label="发布数量">
          <template slot-scope="{ row }">
            <span v-if="!row.isLimited">不限量</span>
            <div v-else>
              <span class="fa">发布：{{ row.total }}</span>
              <span class="sheng">剩余：{{ row.lastTotal }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否开启" min-width="100">
          <template slot-scope="scope" v-if="checkPermi(['admin:coupon:update:status'])">
            <el-switch v-model="scope.row.status" :active-value="true" :inactive-value="false" active-text="开启"
              inactive-text="关闭" @click.native="onchangeIsShow(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" class="mr10" size="small" @click="receive(scope.row)"
              v-hasPermi="['admin:coupon:user:list']">领取记录</el-button>
            <el-button type="text" class="mr10" size="small" @click="sendToUser(scope.row)">发放</el-button>
            <router-link :to="{ path: '/marketing/coupon/list/save/' + scope.row.id }">
              <el-button v-if="scope.row.status" type="text" size="small" class="mr10"
                v-hasPermi="['admin:coupon:info']">复制</el-button>
            </router-link>
            <el-button type="text" class="mr10" size="small" @click="handleDelMenu(scope.row)"
              v-hasPermi="['admin:coupon:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <!--领取记录-->
    <el-dialog title="领取记录" v-dialogDrag :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
      <el-table v-loading="Loading" :data="issueData.data" style="width: 100%">

        <el-table-column prop="nickname" label="用户名" min-width="120" />
        <el-table-column label="用户头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image style="width: 36px; height: 36px" :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机" min-width="120" />

        <el-table-column prop="createTime" label="领取时间" min-width="180" />
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[10, 20, 30, 40]" :page-size="tableFromIssue.limit"
          :current-page="tableFromIssue.page" layout="total, sizes, prev, pager, next, jumper" :total="issueData.total"
          @size-change="handleSizeChangeIssue" @current-change="pageChangeIssue" />
      </div>
    </el-dialog>

    <!-- 发放记录 -->
    <el-dialog title="发放优惠券给用户" :visible.sync="dialogUserVisible" width="900px" :before-close="handleUserClose">

      <el-form :inline="true" :model="tableUserParam" class="demo-form-inline">
        <el-form-item label="查询">
          <el-input v-model="tableUserParam.keywords" size="small" placeholder="请输入姓名或手机号" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getUserList">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="Loading" :data="tableUserData.data" style="width: 100%" @selection-change="userSelect">
        <el-table-column type="selection" width="55" />
        <el-table-column label="姓名" min-width="160">
          <template slot-scope="scope">
            <span>{{ scope.row.nickname | filterEmpty }} | {{ scope.row.sex | sexFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户等级" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.level | levelFilter | filterEmpty }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="nowMoney" label="余额" />
        <el-table-column prop="integral" label="积分" />
        <el-table-column prop="phone" label="手机" min-width="120" />
        <el-table-column prop="nickname" label="昵称" min-width="120" />
        <el-table-column prop="createTime" label="创建时间" min-width="180" />
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[10, 20, 30, 40]" :page-size="tableUserParam.limit"
          :current-page="tableUserParam.page" layout="total, sizes, prev, pager, next, jumper"
          :total="tableUserData.total" @size-change="handleSizeChangeUser" @current-change="pageChangeUser" />
      </div>

      <div>
        <el-button @click="confirmSend" type="primary"> 确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { userListApi } from '@/api/user'
import { marketingListApi, couponIssueStatusApi, couponUserListApi, couponDeleteApi, couponUserApi } from '@/api/marketing'
import { roterPre } from '@/settings'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
export default {
  name: 'CouponList',
  data() {
    return {
      Loading: false,
      dialogVisible: false,
      dialogUserVisible: false,
      roterPre: roterPre,
      listLoading: true,
      tableUserParam: {
        page: 1,
        limit: 10,
      },
      sendForm: {
        couponId: undefined,
        uids: [],
        uid: ''
      },
      tableUserData: {
        data: [],
        total: 0
      },
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        status: '',
        name: ''
      },
      tableFromIssue: {
        page: 1,
        limit: 10,
        couponId: ''
      },
      issueData: {
        data: [],
        total: 0
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    checkPermi,
    seachList() {
      this.tableFrom.page = 1
      this.getList()
    },
    sendToUser(row) {
      this.dialogUserVisible = true
      this.sendForm.couponId = row.id
      this.getUserList()
    },
    handleUserClose() {
      this.dialogUserVisible = false
    },
    userSelect(item) {
      this.sendForm.uids = item.map(item => item.uid)
    },
    getUserList() {
      this.Loading = true
      userListApi(this.tableUserParam).then(res => {
        console.log(res);
        this.tableUserData.data = res.list
        this.tableUserData.total = res.total
        this.Loading = false
      }).catch(() => {
        this.Loading = false
      })
    },
    confirmSend() {

      let obj = { couponId: this.sendForm.couponId, uid: this.sendForm.uids.toString() }
      couponUserApi(obj).then(res => {
        console.log(res);
        this.$message.success("操作成功")
        this.dialogUserVisible = false
        this.sendForm.uids = []
        this.sendForm.couponId = undefined
      })

    },
    handleClose() {
      this.dialogVisible = false
    },
    // 领取记录
    receive(row) {
      this.dialogVisible = true
      this.tableFromIssue.couponId = row.id
      this.getIssueList()
    },
    // 列表
    getIssueList() {
      this.Loading = true
      couponUserListApi(this.tableFromIssue).then(res => {
        this.issueData.data = res.list
        this.issueData.total = res.total
        this.Loading = false
      }).catch(res => {
        this.Loading = false
        this.$message.error(res.message)
      })
    },
    pageChangeUser(page) {
      this.tableUserParam.page = page
      this.getUserList()
    },
    pageChangeIssue(page) {
      this.tableFromIssue.page = page
      this.getIssueList()
    },
    handleSizeChangeIssue(val) {
      this.tableFromIssue.limit = val
      this.getIssueList()
    },
    handleSizeChangeUser(val) {
      this.tableUserParam.limit = val
      this.getUserList()
    },
    // 列表
    getList() {
      this.listLoading = true
      marketingListApi(this.tableFrom).then(res => {
        this.tableData.data = res.list
        this.tableData.total = res.total
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    },
    // 修改状态
    onchangeIsShow(row) {
      couponIssueStatusApi({ id: row.id, status: row.status }).then(() => {
        this.$message.success('修改成功')
        this.getList()
      }).catch(() => {
        row.status = !row.status
      })
    },
    handleDelMenu(rowData) {
      this.$confirm('确定删除当前数据?').then(() => {
        couponDeleteApi({ id: rowData.id }).then(data => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
  }
}
</script>

<style scoped lang="scss">
.selWidth {
  width: 350px !important;
  height: 40px;
}

::v-deep .el-input--medium .el-input__inner {
  height: 40px;
  line-height: 40px;
}

.seachTiele {
  line-height: 40px;
}

.fa {
  color: #0a6aa1;
  display: block;
}

.sheng {
  color: #ff0000;
  display: block;
}
</style>
